﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;

namespace SoundServant
{
    public class Congregation
    {
        int id;
        DayOfWeek bstmssmDay;
        DayOfWeek ptwsDay;
        string name;
        int songbook;
        string songbookName;
        int? tickTock;
        string password;
        bool skypeEnabled;
        DirectoryInfo skypeConfigDir;

        public int Id
        {
            get
            {
                return id;
            }
        }
        public DayOfWeek BstmssmDay
        {
            get
            {
                return bstmssmDay;
            }
            set
            {
                bstmssmDay = value;
            }
        }
        public DirectoryInfo SkypeConfigDir
        {
            get { return skypeConfigDir; }
        }
        public bool SkypeEnabled
        {
            get
            {
                return skypeEnabled;
            }
            set
            {
                skypeEnabled = value;
            }
        }

        public int Songbook
        {
            get
            {
                return songbook;
            }
            set
            {
                songbook = value;
            }
        }

        public string SongbookName
        {
            get
            {
                return songbookName;
            }
        }


        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
                Write();
            }
        }

        public DayOfWeek PtwsDay
        {
            get
            {
                return ptwsDay;
            }
            set
            {
                ptwsDay = value;
            }
        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public Congregation(int i)
        {
            id = i;
            SqlConnection connection = new SqlConnection(Options.Default.DataBaseConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand("SELECT congName, congTMSSMDay, congPTWSDay, congAdminPassword,  congSkypeEnabled, congSongbook FROM congregations WHERE congId = " + id, connection);
            SqlDataReader reader = command.ExecuteReader();
            reader.Read();
            name = (string)reader[0];
            bstmssmDay = (DayOfWeek)reader[1];
            ptwsDay = (DayOfWeek)reader[2];
            password = (string)reader[3];
            skypeEnabled = (bool)reader[4];
            songbook = (int)reader[5];
            skypeConfigDir = new DirectoryInfo(Options.Default.BaseDirectory + "\\" + Options.Default.SkypeConfigDirectory + "\\" + id + "\\Skype");
            reader.Close();
            SqlCommand songbookNameCommand = new SqlCommand("SELECT songbookTitle FROM songbooks WHERE songbookId = " + songbook.ToString(), connection);
            songbookName = songbookNameCommand.ExecuteScalar().ToString();
            connection.Close();
            connection.Dispose();
            SS.TenthOfASecondTimer.Tick += new EventHandler(Tick);
            Logs.Information("Congregation", "Loaded - Id: " + id);
        }

        void Tick(object sender, EventArgs e)
        {
            if (tickTock == 10)
            {
                tickTock = null;
                Write();
            }
            else if (tickTock != null)
            {
                tickTock++;
            }
        }

        public void Write()
        {
            Logs.Information("Congregation", "Updating details - Id: " + id);
            SqlConnection connection = SS.Connection();
            SqlCommand command = new SqlCommand("UPDATE congregations SET congName = '" + name + "', congTMSSMDay = " + (int)bstmssmDay + ", congPTWSDay = " + (int)ptwsDay + ", congSkypeEnabled = '" + skypeEnabled.ToString() + "', congAdminPassword = '" + password + "', congSongbook = '" + songbook + "' WHERE congId = " + id, connection);
            command.ExecuteNonQuery();
            connection.Close();
            connection.Dispose();
            Logs.Information("Congregation", "Details updated - Id: " + id);
        }
    }
}
